package com.dingup.homework.service.impl;

import com.dingup.homework.bean.DO.HwHomeworkTeacherCorrectDO;
import com.dingup.homework.dao.HwHomeworkTeacherCorrectMapper;
import com.dingup.homework.feign.client.msg.PaikSendMsgFeignClient;
import com.dingup.homework.feign.client.user.UserFeignClient;
import com.dingup.homework.service.SendMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by wancheng on 2018/12/5.
 */
@Service
public class SendMsgServiceImpl implements SendMsgService {

    @Autowired
    PaikSendMsgFeignClient paikSendMsgFeignClient;
    @Autowired
    UserFeignClient userFeignClient;
    @Autowired
    HwHomeworkTeacherCorrectMapper hwHomeworkTeacherCorrectMapper;

    @Override
    @Async
    public void sendHomeworkMsgWhenAssignHomework(String userId, String homeworkName, Date expiredTime) {
        String userName = userFeignClient.getUserNameByUserId(userId);
        paikSendMsgFeignClient.sendHomeworkMsgForStudent(userId, userName, homeworkName, expiredTime);

    }

    @Override
    public void sendHomeworkFinishMsgForTeacher(Integer homeworkRecordId, Integer homeworkId) {

        List<HwHomeworkTeacherCorrectDO> isExist = hwHomeworkTeacherCorrectMapper.getFinishOrCorrectHomework(homeworkId);
        if (CollectionUtils.isEmpty(isExist)) {
            List<String> teacherIds = hwHomeworkTeacherCorrectMapper.getTeacherIdByHomeworkRecordId(homeworkRecordId);
            teacherIds.forEach(
                    id -> {
                        String teacherName = userFeignClient.getUserNameByUserId(id);
                        List<String> subjectNames = hwHomeworkTeacherCorrectMapper.getSubjectNameByHomeworkRecordId(homeworkRecordId, id);
                        String subjectName = subjectNames.stream().collect(Collectors.joining(","));
                        String studentName = hwHomeworkTeacherCorrectMapper.getStudentNameByHomeworkRecordId(homeworkRecordId);
                        paikSendMsgFeignClient.sendHomeworkFinishMsgForTeacher(id, teacherName, homeworkId, studentName, subjectName);
                    }
            );

        }

    }
}
